package org.geemp.game.controller;

import org.geemp.game.common.CommonResult;
import org.geemp.game.common.ResultCode;
import org.geemp.game.service.BaseDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JackRen
 * @date 2021-11-29 15:20
 * @description:
 */

@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private BaseDBService baseDBService;

    /**
     * 判断是否成功注册
     * @param request
     * @return
     */
    @RequestMapping(value = "/doRegister",method = RequestMethod.POST)
    public Object doRegister(HttpServletRequest request){
        String ut_userid = request.getParameter("ut_userid");
        if (StringUtils.isEmpty(ut_userid)) {
            return CommonResult.failed(ResultCode.PARAMSBLANK);
        }

        if (checkIsExistByUserName(ut_userid)){
            return CommonResult.failed(ResultCode.EXIST);
        }
        String ut_password = request.getParameter("ut_password");
        String UT_EMAIL = request.getParameter("UT_EMAIL");
        String UT_PERSONID = request.getParameter("UT_PERSONID");
        String sql = "insert into usertable(ut_userid,ut_password,UT_ACCOUNTID,UT_DIV,UT_PERSONID,UT_EMAIL) values('"+ut_userid+"','"+ut_password+"',convert(binary,'"+ut_userid+"'),'0','"+UT_PERSONID+"','"+UT_EMAIL+"')";
        int ret = baseDBService.updateSql(sql);
        if (ret > 0) {
            return CommonResult.success(null,"注册成功！");
        }else{
            return CommonResult.failed();
        }
    }

    /*
     * 验证用户名是否注册
     * @author JackRen
     * @date 2021/11/22
     * @return true:注册  false：未注册
     **/
    private Boolean checkIsExistByUserName(String ut_userid) {
        String sql = "select * from usertable where ut_userid='"+ut_userid+"'";
        int ret = baseDBService.excuteQuerySqlCount(sql);
        if (ret > 0) {
            return true;
        }
        return false;
    }
}
